Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload get() function for Optional type. #9748

Merged
merged 7 commits into from
Feb 16, 2022
Merged

Conversation

yzh119
Copy link
Member

@yzh119 yzh119 commented Dec 15, 2021

Currently Optional don't have its own get() implementation, so if we want to get the internal container pointer of an optional object o with type Optional<T>, we can either use o.value().get() (in the case it's defined) or static_cast<const T*>(o.get()) (because the inherited get() function has no type information). Neither of them are satisfactory if we want to get a pointer with type const T*.

This PR overloads the get() function of Optional, which returns nullptr when the object is not defined, and it's corresponding data pointer otherwise.

cc @junrushao1994 @tqchen

@junrushao
Copy link
Member

Why close this PR? I'm very interested

@yzh119
Copy link
Member Author

yzh119 commented Dec 15, 2021 via email

@yzh119
Copy link
Member Author

yzh119 commented Jan 20, 2022

I think the CI was broken because of some random numerical issue and I'll re-trigger it, @junrushao1994 any further comments on this PR?

@junrushao
Copy link
Member

Yes, it's a bit of flaky. Let me retrigger this

@junrushao
Copy link
Member

@yzh119 Would you like to rebase and retrigger the CI? Thanks!

@yzh119
Copy link
Member Author

yzh119 commented Feb 11, 2022

@junrushao1994 done.

* \return The internal object pointer with container type of T.
* \note This function do not perform not-null checking.
*/
const ContainerType* get() const { return static_cast<ContainerType*>(data_.get()); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that get is not a standard API as per stl style for Optional. While get is a standard stl style name. We might want to have a longer name in this case.

How about GetContainerPtr(use CamelCase to indicate that it is not STL compatible).

The comment can be updated to explain the behavior of nullopt case(return nullptr) and non-nullopt case(returns the container ptr)

Copy link
Member

@junrushao junrushao Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tqchen for the valuable feedback! On the other hand, I would say this PR actually does is to simply improve the Optional::get method to return the underlying type instead of the plain Object pointer, and hence it might make sense to keep the name as it is. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I didn't noticed that ObjectRef also have a get method. I think this sounds good to me then. Perhaps we can just add additional comments about the behavior

@junrushao
Copy link
Member

Thanks @tqchen @yzh119!

@junrushao junrushao merged commit 1da0093 into apache:main Feb 16, 2022
electriclilies pushed a commit to electriclilies/tvm that referenced this pull request Feb 18, 2022
* upd

* simplify

* upd

* fix

* upd

* fix docstring
pfk-beta pushed a commit to pfk-beta/tvm that referenced this pull request Apr 11, 2022
* upd

* simplify

* upd

* fix

* upd

* fix docstring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants